package com.century.sjt.logic;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.activity.Topic.MyPublishTopicActivity;
import com.century.sjt.logic.ImgsAdapter.OnItemClickClass;
import com.century.sjt.EMChat.widget.EaseTitleBar;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;

public class ImgsActivity extends BaseActivity {
	private SjtTitleBar titleBar;
	private ImageButton btnSys;                                     //返回按钮
	private TextView mTitle;                                        //程序标题
	private LinearLayout leftLinLayout;                             //左边的线性布局
	private ImageButton btnInfomation;                          //上方右边按钮
	Bundle bundle;
	FileTraversal fileTraversal;
	GridView imgGridView;
	ImgsAdapter imgsAdapter;
	LinearLayout select_layout;
	Util util;
	RelativeLayout relativeLayout2;
	HashMap<Integer, ImageView> hashImage;
	Button choise_button;
	ArrayList<String> filelist;

	private String content;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photogrally);

		Intent intent  =this.getIntent();
		content = intent.getStringExtra("content");

//		mTitle = (TextView) findViewById(R.id.img_title);
//		mTitle.setText("选择图片");
//		btnSys = (ImageButton) findViewById(R.id.btn_sys);
//		btnSys.setImageResource(R.drawable.ease_mm_title_back);
//		btnSys.setClickable(false);
//		leftLinLayout = (LinearLayout) findViewById(R.id.leftLinLayout);
//		leftLinLayout.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				finish();
//				overridePendingTransition(R.animator.in_from_left, R.animator.out_to_right);
//			}
//		});
//
//		btnInfomation = (ImageButton) findViewById(R.id.btn_information);
//		btnInfomation.setBackgroundResource(R.drawable.selector_btn_ok);
//		btnInfomation.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent intent =new Intent(ImgsActivity.this, MyPublishTopicActivity.class);
//				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				Bundle bundle=new Bundle();
//				bundle.putStringArrayList("files", filelist);
//				intent.putExtras(bundle);
//				intent.putExtra("content",content);
//				startActivity(intent);
//
//			}
//		});

		titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
		//       标题栏
		titleBar.setRightIconResourc(getResources().getString(R.string.icon_select));
		titleBar.setLeftLayoutClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		titleBar.setRightLayoutClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent =new Intent(ImgsActivity.this, MyPublishTopicActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				Bundle bundle=new Bundle();
				bundle.putStringArrayList("files", filelist);
				intent.putExtras(bundle);
				intent.putExtra("content",content);
				startActivity(intent);
			}
		});
		titleBar.setTitle(getResources().getString(R.string.tab_choose_img));

		imgGridView=(GridView) findViewById(R.id.gridView1);
		bundle= getIntent().getExtras();
		fileTraversal=bundle.getParcelable("data");
		imgsAdapter=new ImgsAdapter(this, fileTraversal.filecontent,onItemClickClass);
		imgGridView.setAdapter(imgsAdapter);
		select_layout=(LinearLayout) findViewById(R.id.selected_image_layout);
		relativeLayout2=(RelativeLayout) findViewById(R.id.relativeLayout2);
		choise_button=(Button) findViewById(R.id.button3);
		hashImage=new HashMap<Integer, ImageView>();
		filelist=new ArrayList<String>();
//		imgGridView.setOnItemClickListener(this);
		util=new Util(this);
	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initEvent() {

	}

	@Override
	protected void initData() {

	}

	class BottomImgIcon implements OnItemClickListener {
		
		int index;
		public BottomImgIcon(int index) {
			this.index=index;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			
		}
	}
	
	@SuppressLint("NewApi")
	public ImageView iconImage(String filepath,int index,CheckBox checkBox) throws FileNotFoundException {
		LayoutParams params=new LayoutParams(relativeLayout2.getMeasuredHeight()-10, relativeLayout2.getMeasuredHeight()-10);
		ImageView imageView=new ImageView(this);
		imageView.setLayoutParams(params);
		imageView.setBackgroundResource(R.mipmap.imgbg);
		float alpha=100;
		imageView.setAlpha(alpha);
		util.imgExcute(imageView, imgCallBack, filepath);
		imageView.setOnClickListener(new ImgOnclick(filepath,checkBox));
		return imageView;
	}
	
	ImgCallBack imgCallBack=new ImgCallBack() {
		@Override
		public void resultImgCall(ImageView imageView, Bitmap bitmap) {
			imageView.setImageBitmap(bitmap);
		}
	};
	
	class ImgOnclick implements OnClickListener {
		String filepath;
		CheckBox checkBox;
		public ImgOnclick(String filepath,CheckBox checkBox) {
			this.filepath=filepath;
			this.checkBox=checkBox;
		}
		@Override
		public void onClick(View arg0) {
			checkBox.setChecked(false);
			select_layout.removeView(arg0);
			choise_button.setText(getResources().getString(R.string.text_already_choose)+"("+select_layout.getChildCount()+")"+getResources().getString(R.string.text_zhang));
			filelist.remove(filepath);
		}
	}
	
	ImgsAdapter.OnItemClickClass onItemClickClass=new OnItemClickClass() {
		@Override
		public void OnItemClick(View v, int Position, CheckBox checkBox) {
			String filapath=fileTraversal.filecontent.get(Position);
			if (checkBox.isChecked()) {
				checkBox.setChecked(false);
				select_layout.removeView(hashImage.get(Position));
				filelist.remove(filapath);
				choise_button.setText(getResources().getString(R.string.text_already_choose)+"("+select_layout.getChildCount()+")"+getResources().getString(R.string.text_zhang));
			}else {
				try {
					if(select_layout.getChildCount()<9) {
						checkBox.setChecked(true);
						Log.i("img", "img choise position->" + Position);
						ImageView imageView = iconImage(filapath, Position, checkBox);
						if (imageView != null) {
							hashImage.put(Position, imageView);
							filelist.add(filapath);
							select_layout.addView(imageView);
							choise_button.setText(getResources().getString(R.string.text_already_choose)+"(" + select_layout.getChildCount() + ")"+getResources().getString(R.string.text_zhang));
						}
					}
					else
					{
						Toast.makeText(ImgsActivity.this,getResources().getString(R.string.text_choose_nine_picture),Toast.LENGTH_SHORT).show();
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	};
	
	public void tobreak(View view){
		finish();
	}
	
	/**
	 * FIXME
	 * ��ֻ��Ҫ�����������ѡ�е��ĵ�Ŀ¼��list����ʽ����ȥ����
	 * @param view
	 */
	public void sendfiles(View view){
		Intent intent =new Intent(this, MyPublishTopicActivity.class);
		Bundle bundle=new Bundle();
		bundle.putStringArrayList("files", filelist);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
		
	}
}
